<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

	function get_user_by_user_id($user_id){
		$query = $this->db->select('*')->from('users')->where('user_id',$user_id)->get();
        return $query->row_array();
	}
	
	function get_user_by_user_key($user_key){
		$query = $this->db->select('*')->from('users')->where('user_key',$user_key)->get();
        return $query->row_array();
	}
	
	function get_user_by_user_id_group($user_id){
		$query = $this->db->select('a.*,b.*')->from('users a')->join('auth_groups b','b.id=a.user_group','LEFT')->where('a.user_id',$user_id)->get();
        return $query->row_array();
	}
	
	function get_user_by_username($user_name){
		$query = $this->db->select('*')->from('users')->where('user_name',$user_name)->get();
        return $query->row_array();
	}
	
	function get_user_by_username_group($user_name){
		$query = $this->db->select('a.*,b.*')->from('users a')->join('auth_groups b','b.id=a.user_group','LEFT')->where('a.user_name',$user_name)->or_where('a.user_tel',$user_name)->get();
        return $query->row_array();
	}
	
	function get_user_by_user_group($user_group)
	{
		$query = $this->db->select('title')->from('auth_groups')->where('id',$user_group)->get();
        $info = $query->row_array();
		if($info){
			return $info['title'];
		}else{
			return '未知';
		}
		
	}

	function register($data){
		return $this->db->insert('users',$data);
	}

    function login($data){
	    $user = $this->get_user_by_username($data['user_name']);
	    if($user){
			$password = password_dohash($data['user_password'],$user['user_salt']);
			if($user['user_password']==$password){
				$this->session->set_userdata('user_id',$user['user_id']);
				$loveme = password_dohash($user['user_id'],$user['user_salt']);
				if($loveme){
					$_COOKIE['loveme'] = $loveme;
					setcookie('loveme', $loveme, time() + 86500, '/', '', false, true);
					$this->user_m->update_user($user['user_id'],array('user_key'=>$loveme));
				}
				return TRUE;
			} else {
				return FALSE;
			}
	    } else {
			return FALSE;
		}
    }
	
	function checklogin($key,$salt){
		$user=$this->user_m->get_user_by_user_key($key);
		if($user){
			$this->session->set_userdata('user_id',$user['user_id']);
			$loveme = password_dohash($user['user_id'],$salt);
			if($loveme){
				$_COOKIE['loveme'] = $loveme;
				setcookie('loveme', $loveme, time() + 86500, '/', '', false, true);
				$this->user_m->update_user($user['user_id'],array('user_key'=>$loveme));
			}			
		}
	}
	
	function check_ulock($username){
		$query = $this->db->select('user_lock')->from('users')->where('user_name',$username)->or_where('user_tel',$username)->get();
        $ulock = $query->row_array();
		if($ulock['user_lock']==0){
			return true;
		}else{
			return false;
		}
	}

	function check_register_username($user_name){
		$query = $this->db->select('user_lock')->from('users')->where('user_name',$user_name)->or_where('user_tel',$user_name)->get();
        return $query->row_array();
	}
	
	function update_user($uid, $data){
		$this->db->where('user_id',$uid);
  		$this->db->update('users', $data); 
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	
	public function update_ulognum($uid,$num=1)
	{
		$this->db->set('user_lognum','user_lognum+'.$num,false)->where('user_id',$uid)->update('users');
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	
	public function update_credit($uid,$credit)
	{
		$this->db->set('user_credit','user_credit+'.$credit,false)->where('user_id',$uid)->update('users');
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	
	public function del_user_key_by_user_id($uid)
	{
		$this->db->set('user_key',time(),false)->where('user_id',$uid)->update('users');
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}

    function get_user_with_group($uid){
        $sql = "SELECT * FROM {$this->db->dbprefix}auth_groups g left join {$this->db->dbprefix}auth_group_access a on g.id=a.group_id where a.uid=$uid";
        $query=$this->db->query($sql);
        return $query->row_array();
    }

    public function get_user_group_rule_list($limit,$start){
        $this->db->select('users.*,auth_groups.id as gid,auth_groups.title');
        $this->db->from('users');
        $this->db->where("users.user_group<",3);
        $this->db->join('auth_group_access','users.user_id = auth_group_access.uid','left');
        $this->db->join('auth_groups','auth_groups.id = auth_group_access.group_id','left');
        $this->db->limit($start,$limit);
        $this->db->order_by('users.user_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }

    }

    /**
     * 判断号码是否已注册
     * @param $mobile
     * @return bool
     */
    public function is_mobile_registered($mobile){

        $this->db->select('*');
        $this->db->where("user_tel",$mobile);
        $this->db->from('users');
        $query = $this->db->get();
        return $query->row_array();
    }
    public function update_user_avatar($user_id,$data){
    	$this->db->where('user_id',$user_id);
        $this->db->update('users', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    //修改用户生日
    public function update_user_birth($user_id,$data){
    	$this->db->where('user_id',$user_id);
        $this->db->update('users',$data);
        // echo $this->db->last_query();
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function get_username_by_utel($utel){
        $query = $this->db->select('user_name')->from('users')->where('user_tel',$utel)->get();
        return $query->row_array();
    }
    //查找用户是否绑定QQ
    public function get_qq_user_by_id($user_id){
    	$this->db->select('*');
    	$this->db->from('qq_users');
    	$this->db->where('user_id',$user_id);
    	$query = $this->db->get();
    	return $query->row_array();
    }
    //查找微信用户
    public function get_wx_user_by_id($user_id){
    	$this->db->select('*');
    	$this->db->from('weixin_users');
    	$this->db->where('user_id',$user_id);
    	$query = $this->db->get();
    	return $query->row_array();
    }
    //查找微博登录
    public function get_wb_user_by_id($user_id){
    	$this->db->select('*');
    	$this->db->from('weibo_users');
    	$this->db->where('user_id',$user_id);
    	$query = $this->db->get();
    	return $query->row_array();
    }
    //查询用户的注册手机号
    public function get_user_tel_by_id($user_id){
    	$this->db->select('user_tel');
    	$this->db->from('users');
    	$this->db->where('user_id',$user_id);
    	$query = $this->db->get();
    	return $query->row_array();
    }

    public function update_user_referee($uid,$user_referee)
    {
        $this->db->set('user_referee',$user_referee,false)->where('user_id',$uid)->update('users');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function get_referee_by_uid($uid){
        $this->db->select('users.user_id,users.user_name,users.user_uname,users.user_regtime');
        $this->db->from('users');
        $this->db->where('user_referee',$uid);
        $this->db->order_by('user_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //寻找默认地址
    function get_address_by_uid($user_id){
		$query = $this->db->select('*')->from('user_dress')->where('dress_type',1)->where('user_id',$user_id)->get();
        return $query->result_array();
	}

    /**
     * @param $where
     * @return bool
     * 删除用户
     */
    public function delete_shop_users_by($where){
        $this->db->where($where)->delete('users');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    //查询所有的用户
    public function get_all_users(){
        $query = $this->db->select('user_id,user_name')->from('users')->where('user_group',4)->order_by('user_id','desc')->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    //计算关键字的用户
    public function count_user_find($key=array())
    {
        $this->db->select('user_id');
        $this->db->from('users');
        if($key){
            $this->db->group_start();
            foreach($key as $k => $v){
                $this->db->or_like('user_name', $v);
            }
            $this->db->group_end();
        }
        // $this->db->where('good_lock',0);
        $total=$this->db->count_all_results();
        return $total;  
    }

    //搜索用户
    public function user_find($start, $limit, $key=array())
    {
        $this->db->select('users.*,auth_groups.id as gid,auth_groups.title');
        $this->db->from('fox_users');
        $this->db->join('auth_group_access','users.user_id = auth_group_access.uid','left');
        $this->db->join('auth_groups','auth_groups.id = auth_group_access.group_id','left');
        if($key){
            $this->db->group_start();
            foreach($key as $k => $v){
                $this->db->or_like('user_name', $v);
            }
            $this->db->group_end();
        }       
        // $this->db->where('good_lock',0);
        $this->db->order_by('user_id','desc');
        $this->db->limit($limit,$start);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    function check_register_username_tel($user_name){
        $query = $this->db->select('*')->from('users')->where('user_tel',$user_name)->get();
        return $query->row_array();
    }


    public function get_users_list($limit,$start){
        $this->db->select('users.*');
        $this->db->from('users');
        $this->db->where("users.user_group>",2);
        $this->db->limit($start,$limit);
        $this->db->order_by('users.user_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }

    }

    function get_user_by_user_openid($openid){
        $query = $this->db->select('*')->from('users')->where('wx_openid',$openid)->get();
        return $query->row_array();

    }

    public function get_users_all(){
        $this->db->select('users.user_id,users.user_name');
        $this->db->from('users');
        $this->db->where("users.user_group >",2);
        $this->db->order_by('users.user_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }

    }

}
